package com.laotou99.gjCoin.wallet;

import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.ECGenParameterSpec;
import java.util.Base64;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class Wallet1 {
	public PrivateKey privateKey;
	public PublicKey publicKey;
	public Wallet1(){
		generateKeyPair();
	}
	public void generateKeyPair() {
		try {

	        Security.addProvider(new BouncyCastleProvider());
	        
			KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDSA","BC");
//			KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
//			KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA","BC");
			
			Provider[] providers = Security.getProviders();
			
			for(Provider p1:providers){
				System.out.println(p1.getName()+","+p1.getInfo());
			}
			
			
//			KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
			
			SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
			
			ECGenParameterSpec ecSpec = new ECGenParameterSpec("prime192v1");
			
			keyGen.initialize(ecSpec, random);
//			keyGen.initialize(1024);
			
			KeyPair keyPair = keyGen.generateKeyPair();
			
			privateKey = keyPair.getPrivate();
			System.out.println("pri -=>"+privateKey.hashCode());
			
			byte[] b1 = privateKey.getEncoded();
			
			publicKey = keyPair.getPublic();
			
			byte[] b2 = publicKey.getEncoded();
			
			String str1 = getStringFromKey(privateKey);
			String str2 = getStringFromKey(publicKey);
			System.out.println("privateKey str1-=>"+str1);
			System.out.println("publicKey str2-=>"+str2);
			
		}catch(Exception e) {
			throw new RuntimeException(e);
		}
	}
	@Override
	public String toString() {
		return "Wallet1 [privateKey=" + privateKey + ", publicKey=" + publicKey + "]";
	}
	
	public String toString2() {
		return "Wallet1 [privateKey=" + privateKey.getAlgorithm() + ", publicKey=" + publicKey.getAlgorithm() + "]";
	}
	
	public static String getStringFromKey(Key key) {
		return Base64.getEncoder().encodeToString(key.getEncoded());
	}
	
}
